<%@ page language="java" isELIgnored="false" import="java.util.*"
	pageEncoding="UTF-8" contentType="text/html; charset=UTF-8"%>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>适配器设置</title>
<link href="<%=request.getContextPath() %>/fare/css/base.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="<%=request.getContextPath() %>/fare/js/jquery-1.6.4.min.js"></script>
<script type="text/javascript" src="<%=request.getContextPath() %>/fare/js/datePicker/WdatePicker.js"></script>
<script type="text/javascript" src="<%=request.getContextPath() %>/fare/lib/ligerUI/js/ligerui.min.js"></script>
<link href="<%=request.getContextPath() %>/fare/lib/ligerUI/skins/Aqua/css/ligerui-all.css" rel="stylesheet" type="text/css" />
<link rev="stylesheet" media="all" href="<%=request.getContextPath() %>/fare/css/form_style.css" type="text/css" rel="stylesheet">
<script type="text/javascript" src="<%=request.getContextPath() %>/jsFile/wait/js/wait.js"></script>
<script type='text/javascript'>
	var UNFOLDID=[];//全局变量
	$(function(){
		init();
		ajaxQuery(-1);
	});
	function a(){
		return "FUCK";
	}
	
	function init(){

		var typeData=[{val:'1',text:'可用',color:'green'},{val:'0',text:'不可用',color:'red'}];
		var columns=[   
						
						{display : '类名',	width : 200,name : 'adapterClassName'},
						{display : '当前状态',	name : 'isenable',width : 120,
							render:function(item){
								for(var i=0;i<typeData.length;i++){
									if(typeData[i].val==item.isenable){
										return "<label style='color:"+typeData[i].color+"'>"+typeData[i].text+"</label>";
									}
								}
							}
						},
						{display : '版本',	name : 'version',width : 80},
						{display : '备注',	name : 'remarks',width : 200},
						{display : '创建时间',name : 'creattimeStr',width : 150},
						{display : '最后更新时间',name : 'upDataTimeStr',width : 150},
						{display : '上传类ID', name:	'adapterUploadId',width:1,hide:true},
						{display : '状态控制',width:100,render:function(item){
							var p="";
							for(var i=0;i<typeData.length;i++){
								if(typeData[i].val==item.isenable){
									p+="<option value='"+typeData[i].val+"' selected='selected'>"+typeData[i].text+"</option>";
								}else{
									p+="<option value='"+typeData[i].val+"'>"+typeData[i].text+"</option>";
								}
								
							}
							return "<select style='margin-top:1px;'  onchange=changeEnable('"+item.id+"','"+item.sourceName+"','"+item.sourceID+"','"+item.version+"',this)>"+p+"</select>";
						}},
						{display:"下载到本地",width:100,render:function(item){
							return "<a href=<%=request.getContextPath()%>/adaptorAction!downAdapter.action?adapterFileId="+item.adapterUploadId+">下载</a>"
						}}
		];
		
		
		//本地数据吧，好累
		$("#adapterData_div").ligerGrid({
			
			height:'100%',
			width:'100%',
			toolbarShowInLeft:false,
			enabledEdit: true,
			alternatingRow:true,
			columns:columns,
			sortName:"sourceID",
			groupColumnName:'sourceName',
			/* groupColumnDisplay :"渠道", */
			groupRender:function(name,groupData){
				return "<span id='_spanForToggle_"+groupData[0].sourceID+"'></span>"+"&nbsp渠道 [ "+name+" ] ";
			},
			usePager:false,
			dataAction:"local",
			/* rowHeight:25, */
			toolbarShowInLeft:false,
			enabledSort:false,
			toolbar : {//放弃修改的按钮，修改个屁啊！
     			items : [ {
     				line : true
	     			}, {
	     				text : '增加',
	     				click : function(){toolbarClick("add");},
	     				img : '<%=request.getContextPath()%>/fare/js/ligerUI/skins/icons/add.gif'
	     			},{
	     				line : true
	     			},{
     				text : '删除',
     				click : function(){toolbarClick("delete");},
     				img : '<%=request.getContextPath()%>/fare/js/ligerUI/skins/icons/delete.gif'
     				} ,{line:true}]
     		}
		});
		//工具条点击
		function toolbarClick(action){
			switch(action){
			case 'add':
				toUpAdapterPage();
				break;
			case 'delete':
				deleteAdapter();
				break;
			}
		}
		//跳转到上传适配器页面
		function toUpAdapterPage(){
			var url="<%=request.getContextPath()%>/adaptorAction!toUpAdapterJsp.action";
			var w_w=700;
			var w_h=420;
			var iTop = (window.screen.availHeight-30-w_h)/2; //获得窗口的垂直位置;
			var iLeft = (window.screen.availWidth-10-w_w)/2; //获得窗口的水平位置;
			var obj={parent:window};
			window.showModalDialog(url,obj,
						'dialogHeight='+w_h+
						'px;dialogWidth='+w_w+
						'px;dialogLeft='+iLeft+
						'px;dialogTop='+iTop+
						'px;status=no;scroll=yes;resizable=no;help=no,center=yes'
						); 
		}
		
		function deleteAdapter(){
			var row=$("#adapterData_div").ligerGetGridManager().getSelectedRows();
			if(row==null || row.length==0){
				return;
			}
			if(row.length>1){
				$.ligerDialog.warn('暂不支持多行删除');
				return;
			}
			var q="<div style='text-align:left'>您即将删除 <br>渠道["+row[0].sourceName+"] 版本["+row[0].version+"] <br>适配器文件！<br>不可恢复！<br>是否继续？<div>";
			$.ligerDialog.confirm(q, function (r) {
				if(r){
					ajaxDelete(row[0].id);
				}
			});
		}
		
	}
	
	
	function ajaxQuery(n){
		var url="<%=request.getContextPath()%>/adaptorAction!getList.action";
		$.ajax({
			cache: false,
	        type: "POST",
	        url:url,
	        dataType:"json",
	        data:$("#condition_from").serialize(),
	        async: true,
	        beforeSend:function(){FDCWait.show();},
	        error:function(e){
	        	$.ligerDialog.error('出错了！请联系管理员');
	        },
	        success:function(data){
	        	var manager = $("#adapterData_div").ligerGetGridManager();
	        	manager.set("data",{Rows:data});
	        	unfold(n);
	        },
	        complete:function(){
	        	FDCWait.hide();
	        	$("span.l-grid-group-togglebtn").click(function(){//添加一个方法
        			if(!/.*-close.*/.test($(this).attr('class'))){
        				var value=$(this).next().attr("id").split("_");
        				value=value[value.length-1];
        				addOrRemove("remove",value);
        			}
				});
	        }
		});
	}
	
	function unfold(n){
		if(n==-1){
			UNFOLDID=[];
		}
		if(!UNFOLDID || UNFOLDID.length==0 || n==-1){
			$("span.l-grid-group-togglebtn").click();
			return;
		}
		
		
		$("span.l-grid-group-togglebtn").each(function(index,e){
			$(e).click();
			for(var i=0;i<UNFOLDID.length;i++){
				if($(e).next()[0]===$("#_spanForToggle_"+UNFOLDID[i])[0]){
					if(/.*-close.*/.test($(e).attr('class'))){
						$(e).click();
					}
				}
			}
		}); 
	}
	
	
	function changeEnable(id,channelName,channelId,version,e){
		if($(e).val()==1){
			$(e).val(0);
			var q="<div style='text-align:left'>您即将打开 <br>渠道[ "+channelName+" ] 版本[ "+version+" ] <br>适配器！<br>同时将关闭该渠道其余适配器！<br>是否继续？<div>";
			$.ligerDialog.confirm(q, function (r) {
				if(r){
					ajaxCloseOrOpenAdapter(id,channelId,1);
				}else{
					$(e).val(0);
				}
			});
			
		}else{
			$(e).val(1);
			var q="<div style='text-align:left'>您即将关闭 <br>渠道[ "+channelName+" ] 版本[ "+version+" ] <br>适配器！<br>是否继续？<div>";
			$.ligerDialog.confirm(q, function (r) {
				if(r){
					ajaxCloseOrOpenAdapter(id,channelId,0);
				}else{
					$(e).val(1);
				}
			});
		}
	}
	
	function ajaxCloseOrOpenAdapter(id,channelId,enable){
		var url='<%=request.getContextPath()%>/adaptorAction!closeOrOpenAdapter.action';
		$.ajax({
	        cache: false,
	        type: "POST",
	        url:url,
	        dataType:"html",
	        data:{id:id,isEnable:enable},
	        async: true,
	        beforeSend:function(){
	        	addOrRemove("add",channelId);
	        	FDCWait.show();
	        },
	        error: function() {
	        	$.ligerDialog.error('出错了！请联系管理员');
	        },
	        success: function(data) {
	        	if(data=="success"){
	        		$.ligerDialog.success('修改成功！');
	        	}else{
	        		$.ligerDialog.warn('无法修改！请联系管理员');
	        	}
	        },
	        complete:function(){
	        	FDCWait.hide();
	        	ajaxQuery();
	        }
		});
	}
	
	function addOrRemove(action,id){
		
		switch(action){
		case "add":
			var index=-1;
			$(UNFOLDID).each(function(i,e){
				if(e==id){index=i;return false;}
			});
			if(index==-1){
				UNFOLDID.push(id);
			}
			break;
		case "remove":
			var index=-1;
			$(UNFOLDID).each(function(i,e){
				if(e==id){
					index=i;
					return false;
				}
			});
			if(index!=-1){
				UNFOLDID.splice(index,1);
			}
			break;
		}
	}
	
	function ajaxDelete(id){
		
		var url='<%=request.getContextPath()%>/adaptorAction!deleteAdapter.action';
		$.ajax({
	        cache: false,
	        type: "POST",
	        url:url,
	        dataType:"html",
	        data:{id:id},
	        async: true,
	        beforeSend:function(){
	        	FDCWait.show();
	        },
	        error: function() {
	        	$.ligerDialog.error('出错了！请联系管理员');
	        },
	        success: function(data) {
	        	if(data=="success"){
	        		$.ligerDialog.success('删除成功！');
	        	}else{
	        		$.ligerDialog.warn('无法删除！请联系管理员');
	        	}
	        },
	        complete:function(){
	        	FDCWait.hide();
	        	ajaxQuery();
	        }
		});
	}
	
</script>

</head>
<body>
<div class="main main_max" style="padding:0px;margin:0px;width:100%">
	<div class="form_body" style='width:100%;margin:0px;padding:0px;'>
		<div class="main_max" style='width:100%'>
			<div class="form_table_tltle">适配器查询</div>
			<form  method="post" id='condition_from'>
			<table cellSpacing=0 cellPadding=0 width="100%" border=0 class="form_table">
				<tr>
					<td class="left">抓取渠道</td>
					<td class="" style="width:450px;">
						<select name='sourceID'>
							<option value="">-- 全部 --</option>
							<c:forEach var="item" items="${sources}">
								<option value="${item.id}">${item.name}</option>
							</c:forEach>
						</select>
					</td>
					<td class="left" >状态</td>
					<td>
						<select name='isEnable'>
							<option value=''>-- 全部 --</option>
							<option value="1">可用 </option>
							<option value="0">不可用</option>
						</select>
					</td>
				</tr>
				<tr>
					<td colspan="4" style='text-align: center'>
						<input type="button" onclick="ajaxQuery(-1)" value="" class="btn01" style='margin:3px 0px 3px 0px'/>
					</td>
				</tr>
			</table>
			</form>
			
			<%-- ligerUI grid --%>
			
			<div id='adapterData_div'></div>
		</div>
	</div>
</div>
</body>
</html>

